package com.mew.chess.core.game.yxx.game.task;


import com.mew.chess.core.common.handler.ChessPushMessageUtil;
import com.mew.chess.core.common.handler.GameCommonService;
import com.mew.chess.core.common.utils.ChessGameUtil;
import com.mew.chess.core.game.yxx.game.YxxGameManager;
import com.mew.chess.core.game.yxx.game.YxxRoomManager;
import com.mew.chess.core.game.yxx.game.common.YxxConstantValue;
import com.mew.chess.core.game.yxx.game.common.YxxLinkType;
import com.mew.chess.core.game.yxx.game.message.YxxPushCMDMapType;
import com.mew.chess.core.game.yxx.game.message.YxxPushStartGameMessage;
import com.mew.chess.core.game.yxx.game.message.YxxUserMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

/**
 * TODO
 * 1, 如果X把不押注 则请出房间
 * 2, 如果服务器已关闭，则清楚房间；
 * 3，开始清数据，
 */
@Component
@Slf4j
public class YxxStartBetTask {

    @Autowired
    private GameCommonService gameCommonService;

    public boolean startBet(int roomId){
        YxxRoomManager roomManager = YxxGameManager.getInstance().getController(roomId);
        roomManager.getOnlineUser().forEach((k, session) -> {
            if (null == session || session.isWeakClose()){
                roomManager.clearOnlineUser(k);
            }
        });

        gameCommonService.checkTMSOnline(roomManager.getOnlineUser().values().stream().collect(Collectors.toList()), YxxGameManager.getInstance().getGameId(), roomId);

        if (roomManager.getOnlineUser().isEmpty()){
            log.info("Yxx roomId {} not real user is stop ", roomId);
            roomManager.stopGame();
            return false;
        }
//
//        TmsServerInfo tmsServerInfo = TmsConfigInfoManager.getInstance().getServerInfo();
//        if (tmsServerInfo.getOnline() < OnlineType.Online.value()){
//            log.info("Yxx Start TMS not online {}", tmsServerInfo.toString());
//            roomManager.stopGame();
//            roomManager.getOnlineUser().forEach((k, v) -> {
//                aucRedisService.offlineSession(k);
//                ChessPushMessageUtil.pushMessage(v, BasisCommandCode.OVERHAULED.cmd(), new EmptyMessage());
//            });
//        }

        List<YxxUserMessage> bigRList = roomManager.getBigRList();
        //------start game-----
        startGameLinkType(roomManager);

        sendStartMsg(roomManager,bigRList);

        return true;
    }

    public void startGameLinkType(YxxRoomManager roomManager){
        roomManager.setRuleTime(YxxConstantValue.START_TIME);
        roomManager.setLinkType(YxxLinkType.Start.code());
        roomManager.setStartTime(System.currentTimeMillis());
        roomManager.setIno(ChessGameUtil.generateINO(roomManager.getIno()));
    }

    private void sendStartMsg(YxxRoomManager roomManager,List<YxxUserMessage> bigRList){

        YxxPushStartGameMessage startGameMessage = YxxPushStartGameMessage.builder()
                .remainTime(YxxConstantValue.START_TIME)
                .ino(roomManager.getIno())
                .bigRList(bigRList)
                .build();
        log.info("4525:"+startGameMessage);
        roomManager.getOnlineUser().forEach((k, session) -> {
            ChessPushMessageUtil.pushMessage(session, YxxPushCMDMapType.StartGameMessage.code(), startGameMessage);
        });
    }

}
